<?php
include_once '../samepart/header.php';
?>
<title>No.2商城-购物车添加成功</title>
<div class="container" style="height:100%;">
    <div class="row" style="width: 30%;margin: 0 auto;padding-top: 100px;">
        <div class="panel panel-success">
            <div class="panel-heading">
                <h3 class="panel-title">购物车添加成功提示</h3>
            </div>
            <div class="panel-body">
                <h3 class="text-default">
                    <span class="glyphicon glyphicon-ok-sign"></span>
                    &nbsp;&nbsp;&nbsp;&nbsp;添加购物车成功!</h3>
                <hr/>
                <a id="showCart" href="" class="btn btn-primary">查看购物车</a>&nbsp;&nbsp;&nbsp;&nbsp;
                <a href="../<?php echo $_GET["purl"] ?>" class="btn btn-default">继续购物</a>
            </div>
        </div>
    </div>
</div>

<?php include_once '../samepart/footer.php' ?>

<script type="text/javascript">
    /**
     * getUserId
     */
    function getUserId() {
        var uid = '';
        $.ajax({
            async: false,//取消异步执行，为同步执行。进而被其他方法调用
            url: "http://localhost:80/phalapi/public/index.php?s=App.User.Login",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                'username': '<?php echo $_SESSION["username"] ?>',
                "password": '<?php echo $_SESSION["password"] ?>'
            }),
            dataType: "JSON",
            success: function (result) {
                uid = result['data']['userid'];

                // console.log("uid:"+uid)
            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
        return uid;
    }

    /**
     * getCartId
     */
    function getCartId() {
        var carid = ''
        $.ajax({
            async: false,//取消异步执行，为同步执行。进而被其他方法调用
            url: "http://localhost:80/phalapi/public/index.php?s=App.Cart.getCartInfoByUid",
            type: "GET",
            data: {
                "user_id": getUserId()
            },
            dataType: "JSON",
            success: function (result) {
                carid = result.data.info[0].id;
                // console.log("data:" + username_data)

            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
        return carid;
    }

    /**
     * 添加购物车调用函数
     */
    function addCartDetailInfo() {
        $.ajax({
            async: false,
            url: "http://localhost:80/phalapi/public/index.php?s=App.CartDetail.insertCartDetailInfo",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                'cart_id': getCartId(),
                "good_id": '<?php echo $_GET["gid"] ?>',
                "good_num": 1,
                "good_money": '<?php echo $_GET["price"] ?>',
            }),
            dataType: "JSON",
            success: function (result) {
                // cdid = result['data'];
                // alert("cdid:" + cdid)
                // console.log("cdid:" + cdid)
            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
    }

    function updateCartDetailInfo(cnum, cprice, ccaid) {
        var num = cnum + 1;
        var price = cprice * num;
        console.log("num:" + num, "price:" + price)
        $.ajax({
            async: false,
            url: "http://localhost:80/phalapi/public/index.php?s=App.CartDetail.updateCartDetailInfoById",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                'good_num': num,
                "good_money": price,
                "cartDetail_id": ccaid,
            }),
            dataType: "JSON",
            success: function (result) {
                var cdid = result.data.info;
                // alert("cdid:" + cdid)
                console.log("cdid:" + cdid)
            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
    }


    /**
     * 查重
     */
    $(function () {
        $.ajax({
            url: "http://localhost:80/phalapi/public/index.php?s=App.CartDetail.getCartDetailInfoByCidAndGid",
            type: "GET",
            data: {
                "cart_id": getCartId(),
                "good_id": '<?php echo $_GET["gid"] ?>'
            },
            dataType: "JSON",
            success: function (result) {
                var le = result.data.info.length;
                $("#showCart").attr("href", "cart.php?show_cart_id=" + getCartId())
                if (le == 0) {
                    addCartDetailInfo();
                } else {
                    var caid = result.data.info[0].id
                    var num = result.data.info[0].num
                    var price = result.data.info[0].money
                    updateCartDetailInfo(num, price, caid);
                }
                // console.log("data:" + le)
            },
            error: function (msg) {
                alert("错误信息：" + msg);
            }
        })
    })

</script>